package com.yxsd.web.controller.admin;

import com.pandawork.core.exception.SSException;
import com.pandawork.core.log.LogClerk;
import com.pandawork.core.util.Pagination;
import com.yxsd.common.annotation.Module;
import com.yxsd.common.entity.Member;
import com.yxsd.common.enums.ModuleEnums;
import com.yxsd.common.utils.WebConstants;
import com.yxsd.web.spring.AbstractController;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * AdminMemberController
 *
 * @author: zhangteng
 * @time: 2015/1/5 21:10
 */
@Module(value = ModuleEnums.AdminMember)
@Controller
@RequestMapping(value = "admin/member")
public class AdminMemberController extends AbstractController {

    @Module(value = ModuleEnums.AdminMemberList)
    @RequestMapping(value = {"", "list"}, method = RequestMethod.GET)
    public String list(@RequestParam(value = "pageNo", required = false) Integer pageNo,
                       @RequestParam(value = "search", required = false) String search,
                       Model model) {
        try {
            if (pageNo == null) {
                pageNo = 1;
            }
            if (search == null || search.equals("") || search.length() == 0) {
                search = null;
            }
            int dataCount = memberService.queryCountByCondition(search);
            Pagination page = new Pagination(dataCount, this.pageSize, pageNo);
            List<Member> memberList = memberService.queryByCondition(page, search);

            model.addAttribute("memberList", memberList);
            model.addAttribute("page", page);
            model.addAttribute("search", search);
            return "admin/member/list";
        } catch (SSException e) {
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
    }

    @Module(value = ModuleEnums.AdminMemberNew)
    @RequestMapping(value = "new", method = RequestMethod.GET)
    public String toInsert() {
        return "admin/member/new";
    }

    @Module(value = ModuleEnums.AdminMemberNew)
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String insert(Member member) {
        try {
            memberService.insert(member);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "redirect:/admin/member";
    }

    @Module(value = ModuleEnums.AdminMemberEdit)
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String toUpdate(@PathVariable("id") Integer id,
                           Model model) {
        try {
            Member member = memberService.queryById(Member.class, id);
            model.addAttribute("member", member);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "admin/member/edit";
    }

    @Module(value = ModuleEnums.AdminMemberEdit)
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public String update(Member member) {
        try {
            memberService.update(member);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
        return "redirect:/admin/member";
    }

    @RequestMapping(value = "del", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject del(@RequestParam("id") Integer id) {
        try {
            Member member = new Member();
            member.setId(id);
            memberService.delete(member);
        } catch (SSException e) {
            LogClerk.errLog.error(e);
            sendErrMsgAndErrCode(e);
        }
        return sendSuccessMsg();
    }
}
